package com.fadadianqi.logistics.customer.dao.ibatis;

import com.fadadianqi.commons.GenericDAOSuport;
import com.fadadianqi.logistics.customer.dao.CustomerDAO;
import com.fadadianqi.logistics.customer.model.Customer;
import com.yeqiangwei.commons.util.DateTimeUtils;
import com.yeqiangwei.commons.util.Pagination;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CustomerDAOImpl extends GenericDAOSuport<String, Customer> implements CustomerDAO {

    @Override
    protected Class<?> getObjectClass() {
        return Customer.class;
    }

    @Override
    protected String getTableName() {
        return "customer";
    }

    @Override
    public Customer getByNameAndVillageId(String customerName, String villageId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Customer.PROP_NAME, customerName);
        map.put(Customer.PROP_VILLAGE_ID, villageId);
        return super.getByPropertys(map);
    }

    @Override
    public List<Customer> findAllByVillageId(String villageId,
                                             Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Customer.PROP_VILLAGE_ID, villageId);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

    @Override
    public List<Customer> findAllNotIndexed(Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Customer.PROP_INDEXED, Boolean.FALSE);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

    @Override
    public Integer countAll() {
        return super.countAll(null);
    }

    @Override
    public Integer countByVillageId(String villageId) {
        Map<String, Object> elements = new HashMap<String, Object>();
        elements.put("villageId", villageId);
        return super.countAll(elements);
    }

    @Override
    public int countByTownId(String townId) {
        Map<String, Object> elements = new HashMap<String, Object>();
        elements.put("townId", townId);
        return super.countAll(elements);
    }

    @Override
    public void updateTownIdByVillageId(String townId, String villageId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("townId", townId);
        map.put("villageId", villageId);
        getSqlMapClientTemplate().update(getNamespace() + ".updateTownIdByVillageId", map);
    }

    @Override
    public int countAllNotIndexed() {
        long stime = DateTimeUtils.now();
        Integer i = (Integer) getSqlMapClientTemplate().queryForObject(getNamespace() + ".countAllNotIndexed");
        getLog().info("countAll(Map map): proceed in " + (DateTimeUtils.now() - stime));
        return i;
    }

    @Override
    public List<Customer> findAllByTownId(String townId, Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Customer.PROP_TOWN_ID, townId);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

}
